﻿package com.sh.game.loader
{
    import com.core.destroy.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class ActionReader extends Object
    {
        private var _loader:Loader;
        private var _name:String;
        private var callBack:Function;
        public var actions:Dictionary;
        private var _xml:XML;
        private var _args:Array;
        private var _bitmapdata:BitmapData;

        public function ActionReader()
        {
            return;
        }// end function

        public function get bitmapdata() : BitmapData
        {
            return this._bitmapdata;
        }// end function

        public function get xml() : XML
        {
            return this._xml;
        }// end function

        public function read(param1:ByteArray, param2:String, param3:Function, param4:Array) : void
        {
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:String = null;
            var _loc_13:Array = null;
            var _loc_14:int = 0;
            var _loc_15:Array = null;
            var _loc_16:int = 0;
            var _loc_17:int = 0;
            var _loc_18:int = 0;
            var _loc_19:int = 0;
            var _loc_20:int = 0;
            var _loc_21:int = 0;
            var _loc_22:ByteArray = null;
            var _loc_23:String = null;
            var _loc_24:String = null;
            if (param1 == null)
            {
                param3.apply(null, param4.concat(null));
                this.dispose();
                return;
            }
            this._args = param4;
            this._name = param2;
            this.callBack = param3;
            var _loc_5:* = new ByteArray();
            this.actions = new Dictionary();
            this.actions["resname"] = param1.readUTFBytes(24);
            var _loc_6:* = param1.readShort();
            var _loc_7:int = 0;
            while (_loc_7 < _loc_6)
            {
                
                _loc_10 = param1.readInt();
                _loc_11 = param1.position;
                _loc_12 = param1.readUTFBytes(24);
                _loc_13 = new Array();
                _loc_13.push(param1.readByte());
                _loc_14 = param1.readInt();
                _loc_15 = new Array();
                _loc_16 = param1.position + _loc_14;
                if (_loc_14 > 0)
                {
                    _loc_18 = 0;
                    while (_loc_18 < _loc_13[0])
                    {
                        
                        _loc_19 = param1.readInt();
                        _loc_20 = param1.readInt();
                        _loc_21 = param1.readInt();
                        _loc_22 = new ByteArray();
                        param1.readBytes(_loc_22, 0, _loc_21);
                        if (param1.position > _loc_16 && _loc_18 < (_loc_13[0] - 1))
                        {
                            break;
                        }
                        _loc_15.push({frameX:_loc_19, frameY:_loc_20, bytes:_loc_22});
                        if (param1.position == _loc_16)
                        {
                            break;
                        }
                        _loc_18++;
                    }
                    param1.position = _loc_16;
                }
                _loc_13.push(param1.readShort());
                _loc_13.push(param1.readShort());
                _loc_13.push(param1.readShort());
                _loc_13.push(param1.readShort());
                _loc_13.push(param1.readShort());
                _loc_13.push(_loc_15);
                _loc_17 = param1.readShort();
                if (_loc_17 >= 0)
                {
                    _loc_23 = param1.readUTFBytes(_loc_10 - param1.position + _loc_11);
                    _loc_13.push({soundFrame:_loc_17, sound:_loc_23});
                }
                this.actions[_loc_12] = _loc_13;
                _loc_7++;
            }
            var _loc_8:* = param1.readShort();
            if (param1.readShort() > 0)
            {
                _loc_24 = param1.readUTFBytes(24);
                this.actions["beattackSound"] = _loc_24;
            }
            var _loc_9:* = param1.readInt();
            param1.readBytes(_loc_5, 0, _loc_9);
            this._xml = new XML(param1.readUTFBytes(param1.bytesAvailable));
            if (!this._loader)
            {
                this._loader = new Loader();
            }
            this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.swfLoadComplete);
            this._loader.loadBytes(_loc_5);
            param1.clear();
            _loc_5.clear();
            return;
        }// end function

        protected function swfLoadComplete(event:Event) : void
        {
            var _loc_3:Bitmap = null;
            var _loc_4:Class = null;
            var _loc_2:* = event.currentTarget as LoaderInfo;
            if (_loc_2.content is Bitmap)
            {
                _loc_3 = _loc_2.content as Bitmap;
                this._bitmapdata = _loc_3.bitmapData;
                _loc_2.removeEventListener(Event.COMPLETE, this.swfLoadComplete);
                this.callBack.apply(null, this._args.concat(this));
                this._args = null;
            }
            else if (_loc_2.applicationDomain.hasDefinition(this._name))
            {
                _loc_4 = _loc_2.applicationDomain.getDefinition(this._name) as Class;
                this._bitmapdata = new _loc_4;
                _loc_2.removeEventListener(Event.COMPLETE, this.swfLoadComplete);
                this.callBack.apply(null, this._args.concat(this));
                this._args = null;
            }
            else
            {
                this.callBack.apply(null, this._args.concat(null));
                this.dispose();
            }
            return;
        }// end function

        public function dispose() : void
        {
            if (this._bitmapdata)
            {
                this._bitmapdata.dispose();
                this._bitmapdata = null;
            }
            if (this.actions)
            {
                DestroyUtil.destroyMap(this.actions);
                this.actions = null;
            }
            if (this.callBack != null)
            {
                this.callBack = null;
            }
            this._xml = null;
            if (this._args)
            {
                this._args.length = 0;
                this._args = null;
            }
            this._name = null;
            if (this._loader)
            {
                try
                {
                    this._loader.unload();
                }
                catch (e:Error)
                {
                }
                this._loader = null;
            }
            return;
        }// end function

    }
}
